package com.masterclient.app.businessmodule.minemodule.cash.contract;

import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.R;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceCashBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceGainBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceOrderBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/10/13
*/
public class CashPresenter extends CashContract.Presenter {

    public List<BalanceGainBean.DataBean.ShItemsBean> gainList;
    @Override
    public void getBalanceGain(String page, String page_size,boolean isRefresh) {
        if (gainList == null || gainList.size() <= 0) {
            if (!isRefresh) {
                mView.baseShowPageLoading(false);
            }
        }
        Observable<BalanceGainBean> observable = RetrofitUtil.getInstance().getBalanceGain(page,page_size);
        Disposable disposable = toSubscribe(observable, new Consumer<BalanceGainBean>() {
            @Override
            public void accept(@NonNull BalanceGainBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                if("0".equals(bean.getCode())){
                    mView.showBalanceGain(bean.getData());
                    if (gainList != null && gainList.size() > 0) {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    } else {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NO_DATA, R.mipmap.null_data,"您还没有获取记录~");
                    }
                } else {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    mView.loadFailed();
                    ToastUtil.showToast(WEB_FAILED_STR);
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                mView.loadFailed();
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        if(mRxManager != null){
            mRxManager.add(disposable);
        } else{
            mView.loadFailed();
        }
    }

    public List<BalanceOrderBean.DataBean.ShItemsBean> orderList;
    @Override
    public void getBalanceOrder(String page, String page_size,boolean isRefresh) {
        if (orderList == null || orderList.size() <= 0) {
            if (!isRefresh) {
                mView.baseShowPageLoading(false);
            }
        }
        Observable<BalanceOrderBean> observable = RetrofitUtil.getInstance().getBalanceOrder(page,page_size);
        Disposable disposable = toSubscribe(observable, new Consumer<BalanceOrderBean>() {
            @Override
            public void accept(@NonNull BalanceOrderBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                if("0".equals(bean.getCode())){
                    mView.showBalanceOrder(bean.getData());
                    if (orderList != null && orderList.size() > 0) {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    } else {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NO_DATA, R.mipmap.null_data,"您还没有购物支出~");
                    }
                }else {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    mView.loadFailed();
                    ToastUtil.showToast(WEB_FAILED_STR);
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                mView.loadFailed();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        mRxManager.add(disposable);
    }

    public List<BalanceCashBean.DataBean.ShItemsBean> cashList;
    @Override
    public void getBalanceCash(String page, String page_size,boolean isRefresh) {
        if (cashList == null || cashList.size() <= 0) {
            if (!isRefresh) {
                mView.baseShowPageLoading(false);
            }
        }
        Observable<BalanceCashBean> observable = RetrofitUtil.getInstance().getBalanceCash(page,page_size);
        Disposable disposable = toSubscribe(observable, new Consumer<BalanceCashBean>() {
            @Override
            public void accept(@NonNull BalanceCashBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                if("0".equals(bean.getCode())){
                    mView.showBalanceCash(bean.getData());
                    if (cashList != null && cashList.size() > 0) {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    } else {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NO_DATA, R.mipmap.null_data,"您还没有提现记录~");
                    }
                }else {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                    mView.loadFailed();
                    ToastUtil.showToast(WEB_FAILED_STR);
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                mView.loadFailed();
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        if(mRxManager != null){
            mRxManager.add(disposable);
        }
    }


}